package com.qdlc.p2p.biz.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.MessageService;
import com.qdlc.p2p.common.util.BeanCopierUtils;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dao.MessageDao;
import com.qdlc.p2p.dal.dao.UserNoticeConfigDao;
import com.qdlc.p2p.dal.dto.Message;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.dto.UserNoticeConfig;
import com.qdlc.p2p.dal.model.MessageModel;
import com.qdlc.p2p.dal.util.UserUtils;

@Service("messageService")
public class MessageServiceImpl implements MessageService {

    @Resource
    private MessageDao messageDao;

    @Resource
    private UserNoticeConfigDao userNoticeConfigDao;

    @Override
    public int unreadCount(long receive_user) {
        return messageDao.unreadCount(receive_user);
    }

    @Override
    public PageDataList<MessageModel> receiveList(long userId, int startPage) {
        return messageDao.receiveList(userId, startPage);
    }

    @Override
    public PageDataList<MessageModel> sentList(long userId, int startPage) {
        return messageDao.sentList(userId, startPage);
    }

    @Override
    public MessageModel find(long id, long userId) {
        Message message = messageDao.find(id, userId);
        if (message.getStatus() == 0) {
            messageDao.setRead(id, userId);
        }

        MessageModel model = new MessageModel();
        BeanCopierUtils.copyProperties(message, model);
        User user = UserUtils.getUserById(userId);
        model.setSentUserName(user.getUserName());
        model.setReceiveUserName(user.getUserName());
        return model;
    }

    @Override
    public Message add(Message msg) {
        return messageDao.insert(msg);
    }

    @Override
    public void set(int type, Object[] ids, long userId) {
        switch (type) {
            case 1:
                messageDao.delReceiveList(ids, userId);
                break;
            case 2:
                messageDao.delSentList(ids, userId);
                break;
            case 3:
                messageDao.setUnreadList(ids, userId);
                break;
            case 4:
                messageDao.setReadList(ids, userId);
                break;
            default:

                break;
        }
    }

    @Override
    public void reply(Message m) {
        messageDao.update(m);
    }

    @Override
    public PageDataList<Message> messageList(Message model, int pageNumber, int pageSize) {
        PageDataList<Message> pageDataList = messageDao.findByTitle(model.getTitle().trim(), pageNumber, pageSize);
        return pageDataList;
    }

    @Override
    public void noticeUserSet(List<UserNoticeConfig> list, User user) {
        userNoticeConfigDao.update(list);
    }

    @Override
    public List<UserNoticeConfig> getAllUNConfigs(long userId) {
        List<UserNoticeConfig> allUNConfigs = userNoticeConfigDao.getAllUNConfigs(userId);
        if (allUNConfigs != null && allUNConfigs.size() > 0) {
            return allUNConfigs;
        }
        return null;
    }
}
